package com.usernet.product.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.usernet.product.comm.BaseDao;
import com.usernet.product.entity.Users;
import com.usernet.product.utils.Page;
import com.usernet.product.utils.PageResult;

public class UserDao extends BaseDao {

	/**
	 * return "userId"：登录成功，"-1"：用户名或邮箱不存在，"-2"：密码错误，"-3"：用户已被锁定，"-4"：服务器错误
	 */
	@SuppressWarnings("unchecked")
	public int login(String loginName, String password) {
		Session session = getSession();
		String hql = "from Users where loginName=:name or email=:name";
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setString("name", loginName);
			List list = query.list();
			tr.commit();
			Users user = null;
			if (list.size() > 0)
				user = (Users) list.get(0);
			if (user == null) {
				return -1;
			}
			if (!user.getPassword().equals(password)) {
				return -2;
			}
			if (user.getStatus() == 1) {
				return -3;
			} else {
				return user.getId();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
			e.printStackTrace();
			return -4;
		}
	}

	/**
	 * return "1"：注册成功，"-1"：用户名已存在，"-2"：邮箱已存在，"-3"：昵称已存在，"-4"：服务器错误
	 */
	@SuppressWarnings("unchecked")
	public int reg(String loginName, String password, String email, String tel,
			String niceName) {
		Session session = getSession();
		String hql = "from Users where loginName=:name";
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setString("name", loginName);
			List list = query.list();
			if (list.size() > 0)
				return -1;
			hql = "from Users where email=:email";
			query = session.createQuery(hql);
			query.setString("email", email);
			list = query.list();
			if (list.size() > 0)
				return -2;
			hql = "from Users where niceName=:niceName";
			query = session.createQuery(hql);
			query.setString("niceName", niceName);
			list = query.list();
			if (list.size() > 0)
				return -3;
			Users user = new Users();
			user.setLoginName(loginName);
			user.setPassword(password);
			user.setTel(tel);
			user.setStatus(0);
			user.setEmail(email);
			user.setNiceName(niceName);
			user.setTime(new Timestamp(System.currentTimeMillis()));
			session.save(user);
			tr.commit();
			return 1;
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
			e.printStackTrace();
			return -4;
		}
	}

	@SuppressWarnings("unchecked")
	public PageResult<Users> getAllByPage(Page page) {
		String hql = "from Users order by id";
		return super.getAllByPageAndHql(page, hql);
	}

	@SuppressWarnings("unchecked")
	public PageResult<Users> getAllByParam(Page page, String loginName) {
		String hql = "from Users where loginName like '%" + loginName
				+ "%' order by id";
		return super.getAllByPageAndHql(page, hql);
	}
	/** 检查登录名是否存在 */
	@SuppressWarnings("unchecked")
	public boolean checkLoginName(String loginName) {
		Session session = getSession();
		String hql = "from Users where loginName=:loginName";
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setString("loginName", loginName);
			query.setCacheable(true); // 使用缓存
			List list = query.list();
			tr.commit();
			if (list.size() > 0)
				return true;
			return false;
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
			throw e;
		}
	}
}
